# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

from selenium import webdriver
from selenium.webdriver import ActionChains
import unittest
from time import sleep
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait



class testPageClickClass(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        print("page click start")
        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.driver.quit()
        print("page click end")

    def testClick(self):
        self.driver.get("http://www.ablesky.com")
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        size = self.driver.get_window_size()
        pos = self.driver.get_window_position()
        print(size)
        print(pos)

        eles = self.driver.find_elements_by_class_name("app-header__logo")
        ActionChains(self.driver).move_to_element(eles[0])
        i = pos["x"] + 10
        j = pos["y"] + 10
        moveSize = 5;

        for j in range(pos["y"] + 1, size["height"] + pos["y"], 50):
            if j > pos["y"] + size["height"]:
                break;
            try:
                for i in range(pos["x"] + 1, size["width"] + pos["x"], 50):
                    try:
                        if i > pos["x"] + size["width"]:
                            break
                            pass

                        ActionChains(self.driver).move_by_offset(50, 0).click().perform()
                        # i = i + 5

                        if self.driver.current_url != "https://web.ablesky.com/":
                            sleep(3)
                            print(i, j)
                            print("cur url", self.driver.current_url)
                            self.driver.back()
                            WebDriverWait(self.driver, 10, 1).until(self.driver.current_url == "https://web.ablesky.com/")
                        print(i, j)
                        # sleep(1)
                        pass
                    except MoveTargetOutOfBoundsException as e:
                        print("out of bound1 , pos = ", i, j, e)
                        ActionChains(self.driver).move_by_offset(moveSize, 0).perform()
                        i = i + moveSize
                        pass

                    pass
                ActionChains(self.driver).move_by_offset( -size["width"] + 50, 50)
                i = pos["x"]
                # j = j + 5
                pass
            except MoveTargetOutOfBoundsException as e:
                print("out of bound2 , pos = ", i, j, e)
                ActionChains(self.driver).move_by_offset(moveSize, 0).perform()
                i = i + moveSize
                pass
        pass




        #
        # for i in range(pos["x"] + 1, size["width"] + pos["x"]):
        #     if i != pos["x"] + 1:
        #         ActionChains(self.driver).move_by_offset(5, -size["width"] + 10).click().perform()
        #         i = i+5
        #     for j in range(pos["y"] + 1, size["height"] + pos["y"]):
        #         try:
        #             # perform 表示执行之间加入队列的操作
        #             # ActionChains(self.driver).move_by_offset(i, j).perform()
        #             ActionChains(self.driver).move_by_offset(0, 5).click().perform()
        #             j = j+5
        #             if self.driver.current_url != "https://web.ablesky.com/":
        #                 sleep(3)
        #                 print(i, j)
        #                 print("cur url",self.driver.current_url)
        #                 self.driver.back()
        #                 WebDriverWait(self.driver, 10,1).until(self.driver.current_url=="https://web.ablesky.com/")
        #             print(i, j)
        #             sleep(1)
        #             pass
        #         except MoveTargetOutOfBoundsException as e:
        #             print("err:",e, i, j)
        #         except TimeoutException as e:
        #             print("err:", e, i, j)
        #
        # # js = "document"
        # # self.driver.execute_script(js)
        # sleep(10)



def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    unittest.main(verbosity=2)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
